package com.dodock.banglapapers.server.helper;

import java.io.IOException;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Text;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

public class DDNNewsDetailPoriborton {

	public String Generate(String newsid) throws IOException {
		/*DatastoreService datastore = DatastoreServiceFactory
				.getDatastoreService();
		try {
			Entity detailsEntity = datastore.get(KeyFactory.createKey(
					"NewsDetails", "poriborton__" + newsid));
			Text text = (Text) detailsEntity.getProperty("data");
			String data = Settings.replaceExtras(text.getValue());
			return data;
		} catch (EntityNotFoundException e) {*/
			String url = Settings.WWW_PORIBORTON_COM + newsid.replaceAll(" ", "%20");
			Connection.Response response = Jsoup
					.connect(url)
					.userAgent(
							"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.21 (KHTML, like Gecko) Chrome/19.0.1042.0 Safari/535.21")
					.timeout(30000).execute();

			Document doc = response.parse();
			JsonObject news = new JsonObject();
			news.addProperty("id", newsid);
			news.addProperty("sharelink", url);

			Element element = doc.select("div[class=shNewsDetailsLastCt]").first();
			
			if(!element.select("div[class=shSiteContainer photo_left]").isEmpty()){
				Element imageElementObj = element.select("div[class=shSiteContainer photo_left]").first();
				Elements imageElements = imageElementObj.select("img");
				String image;
				JsonArray imageArray = new JsonArray();
				boolean hasImage = false;
				for (Element imageElement : imageElements) {
					image = imageElement.attr("src").substring(1).trim();
					JsonObject obj = new JsonObject();
					obj.addProperty("url", Settings.WWW_PORIBORTON_COM + image);
					hasImage = true;
					imageArray.add(obj);
				}
				if ((hasImage))
					news.add("gallery", imageArray);
			}
			
			Elements newsDetailElements = element.select("p");
			String details = "";
			for(Element newsElement : newsDetailElements){
				details = details + newsElement.text();
			}
			news.addProperty("details", details
					.replaceAll("\u00a0", "<br/>"));
			
			if (details.length() > 0) {

				String data = news.toString();
				data = Settings.replaceExtras(data);
				/*Text text = new Text(data);
				Entity detailsEntity = new Entity("NewsDetails", "poriborton__"
						+ newsid);
				detailsEntity.setProperty("data", text);
				datastore.put(detailsEntity);*/
				return data;
			} else {
				return "{}";
			}
		}
	//}

}
